package cn.work.control;

import cn.work.bean.reservationBean;
import cn.work.service.ReservationService;
import com.google.gson.Gson;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/*
    加载所有预约数据
 */
@WebServlet(name = "loadAllReservationServlet", value = "/loadAllReservationServlet")
public class loadAllReservationServlet extends HttpServlet {
    reservationBean reservation = new reservationBean();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        List<reservationBean> list = new ArrayList<reservationBean>();
        ReservationService reservationService = new ReservationService();
        try {
            list = reservationService.getAllReservation();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        Gson gson = new Gson();
        PrintWriter out = response.getWriter();
        if (list != null && !list.isEmpty()) {
            gson = new Gson();
            String json = gson.toJson(list);
            out.write(json);
            out.flush();
            out.close();
        } else {
            String json = gson.toJson("没有找到相关预定");
            out.write(json);
            out.flush();
            out.close();
        }
    }
}

